import type { AxiosInstance } from 'axios';

import { client } from '../core';
import { ShareItemControllerApi, ShareItemControllerApiFp } from '../openapi';

export const getDownloadLink = (
  code: string,
  md5: string,
  fileName: string,
) => {
  client.shareItem.getShareItemFiles(code, md5, fileName);
};

export class ShareItemControllerApiExtended extends ShareItemControllerApi {
  async getShareItemFilesLink(code: string, md5: string, fileName: string) {
    const request = await ShareItemControllerApiFp(
      this.configuration,
    ).getShareItemFiles(code, md5, fileName);
    return new Promise<string>((resolve, reject) => {
      request(
        {
          ...this.axios,
          request(config) {
            console.log(config.url);
            if (config.url) {
              resolve(config.url);
            } else {
              reject(new Error('URL is undefined'));
            }
            return null as any; // 这里返回null是为了避免实际发送请求
          },
        } as AxiosInstance,
        this.basePath,
      );
    });
  }
}
